﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Space
{
    public class PirateShip : NewtonShip
    {
        public PirateShip(Game game) : base(game) { }

        public override void Update(float time)
        {
            IData data = Game.Services.GetService<IData>();

            Vector3 destination;

            SolarSystem sol = data.SolarSystems.OrderBy(s => (s.Position - Position).LengthSquared()).First();

            Ship target = (from s in data.Ships
                               where s.Player != Player && s is TradeShip
                               orderby (s.Position - Position).LengthSquared()
                               select s).FirstOrDefault();
            if (target == null)
            {
                float angle = (float)Math.Atan2(sol.Position.Z - Position.Z, sol.Position.X - Position.X);
                destination = sol.Position + sol.Radius * new Vector3((float)Math.Cos(angle), 0, (float)Math.Sin(angle));
            }
            else
            {
                destination = target.Position;
            }

            SetDestination(destination);

            base.Update(time);
        }
    }
}
